<template>
  <el-dialog :title="title" :visible.sync="visible" width="600px" append-to-body v-if="visible">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="日期" prop="date">
        <el-date-picker clearable
                        v-model="form.date"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工作人员" prop="memberId">
        <user-member-select v-model="form.memberId" placeholder="请选择工作人员"/>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input v-model="form.content" type="textarea"/>
      </el-form-item>
      <el-form-item label="图片" prop="images">
        <image-upload v-model="form.images"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>

</template>

<script>
import {addGzrz, getGzrz, updateGzrz} from "@/api/duty/gzrz";

export default {
  name: "rzForm",
  data() {
    return {
      buttonLoading: false,
      title: '',
      visible: false,
      form: {},
      // 表单校验
      rules: {
        gzrzId: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        memberId: [
          { required: true, message: "工作人员不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        images: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
      }
    }
  },
  methods: {
    open(row, memberId) {
      this.loading = true;
      this.reset();
      if (row) {
        const gzrzId = row.gzrzId
        getGzrz(gzrzId).then(response => {
          this.form = response.data;
          this.visible = true;
          this.title = "修改工作日志";
        }).finally(()=> {
          this.loading = false;
        });
      }else {
        if (memberId) {
          this.form.memberId = memberId;
        }
        this.visible = true
      }
    },
    // 取消按钮
    cancel() {
      this.visible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gzrzId: undefined,
        date: undefined,
        memberId: undefined,
        content: undefined,
        images: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.gzrzId != null) {
            updateGzrz(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.visible = false;
              this.$emit('update');
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGzrz(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.visible = false;
              this.$emit('update');
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
